<template>
  <div class="register-view">
    <div class="register-container">
      <!-- 绿色头部区域 -->
      <div class="register-header">
        <div class="header-decoration">
          <div class="circle circle-1"></div>
        </div>
        <h1 class="register-title">创建你的领地</h1>
        <p class="register-subtitle">涂色抢地盘 - 新玩家注册</p>
      </div>
      
      <!-- 白色表单区域 -->
      <div class="register-form">
        <el-form 
          :model="registerForm" 
          :rules="registerRules" 
          ref="registerFormRef" 
          label-position="top"
          @keyup.enter="handleRegister"
        >
          <el-form-item label="玩家名称" prop="username">
            <el-input 
              v-model="registerForm.username" 
              placeholder="+ 起个酷炫的玩家名"
              :prefix-icon="User"
              clearable
              size="large"
            />
          </el-form-item>
          
          <el-form-item label="邮箱地址" prop="email">
            <el-input 
              v-model="registerForm.email" 
              placeholder="■ 输入常用邮箱"
              :prefix-icon="Message"
              clearable
              size="large"
            />
          </el-form-item>
          
          <el-form-item label="设置密码" prop="password">
            <el-input 
              v-model="registerForm.password" 
              type="password"
              placeholder="● 设置登录密码"
              :prefix-icon="Lock"
              clearable
              size="large"
              show-password
            />
          </el-form-item>
          
          <el-form-item label="确认密码" prop="confirmPassword">
            <el-input 
              v-model="registerForm.confirmPassword" 
              type="password"
              placeholder="● 再次输入密码"
              :prefix-icon="Lock"
              clearable
              size="large"
              show-password
            />
          </el-form-item>
          
          <el-form-item prop="agreeTerms">
            <el-checkbox v-model="registerForm.agreeTerms">
              我已阅读并同意
              <el-link type="primary" @click="showTerms">服务条款</el-link>
              和
              <el-link type="primary" @click="showPrivacy">隐私政策</el-link>
            </el-checkbox>
          </el-form-item>
          
          <el-form-item>
            <el-button 
              type="success" 
              size="large" 
              style="width: 100%"
              @click="handleRegister"
              :loading="loading"
              :disabled="!isFormValid"
            >
              <el-icon v-if="!loading"><Plus /></el-icon>
              {{ loading ? '注册中...' : '开疆拓土!' }}
            </el-button>
          </el-form-item>
        </el-form>
        
        <!-- 登录链接 -->
        <div class="login-link">
          <el-link 
            type="primary" 
            @click="goToLogin"
            class="login-text"
          >
            已有领地? 立即登录守卫阵地
          </el-link>
        </div>
        
        <!-- 新手指南 -->
        <div class="newbie-guide">
          <div class="guide-header">
            <el-icon class="guide-icon"><InfoFilled /></el-icon>
            <span class="guide-title">新手指南</span>
          </div>
          <p class="guide-content">
            从空白区域开始涂色是最快扩张的方式!快注册加入战斗吧!
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
// 页面职责：注册新用户，使用 user store 模拟写入并持久化，成功后跳转首页
import { ref, reactive, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Message, Lock, Plus, InfoFilled } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()

// 表单引用与加载态
const registerFormRef = ref()

// 注册表单数据
const registerForm = reactive({ 
  username: 'testuser', 
  email: 'test@example.com', 
  password: '123456', 
  confirmPassword: '123456', 
  agreeTerms: false 
})

// 表单验证状态
const isFormValid = computed(() => 
  registerForm.username.trim() && 
  registerForm.email.trim() && 
  registerForm.password.trim() && 
  registerForm.confirmPassword.trim() && 
  registerForm.agreeTerms
)

// 注册表单验证规则
const registerRules = {
  username: [
    { required: true, message: '请输入玩家名称', trigger: 'blur' },
    { min: 2, max: 20, message: '玩家名称长度在 2 到 20 个字符', trigger: 'blur' },
    { pattern: /^[a-zA-Z0-9\u4e00-\u9fa5_-]+$/, message: '玩家名称只能包含字母、数字、中文、下划线和横线', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    { validator: (rule, value, callback) => { 
      if (value !== registerForm.password) { 
        callback(new Error('两次输入的密码不一致')) 
      } else { 
        callback() 
      } 
    }, trigger: 'blur' }
  ],
  agreeTerms: [
    { validator: (rule, value, callback) => { 
      if (!value) { 
        callback(new Error('请阅读并同意服务条款和隐私政策')) 
      } else { 
        callback() 
      } 
    }, trigger: 'change' }
  ]
}

// 处理注册
const handleRegister = async () => {
  try {
    await registerFormRef.value.validate()
    
    // 清理数据，只发送API需要的字段
    const userData = {
      username: registerForm.username.trim(),
      password: registerForm.password,
      email: registerForm.email.trim()
    }
    
    // 使用userStore的register方法
    await userStore.register(userData)
    
    // 注册成功后跳转到首页
    router.push('/')
  } catch (error) {
    console.error('注册失败:', error)
    // 错误消息已经在store中处理
  }
}

// 显示服务条款 / 隐私政策
const showTerms = () => { ElMessage.info('服务条款功能开发中...') }
const showPrivacy = () => { ElMessage.info('隐私政策功能开发中...') }

// 跳转到登录页面
const goToLogin = () => { router.push('/login') }

// 页面加载时检查登录状态
onMounted(async () => {

})

//
</script>

<style scoped>
.register-view {
  min-height: 100vh;
  height: 100vh;
  background: linear-gradient(135deg, #e3f2fd 0%, #f3e5f5 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  box-sizing: border-box;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.register-container {
  background: white;
  border-radius: 16px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  max-width: 500px;
  width: 100%;
  margin: 0 auto;
  position: relative;
}

/* 绿色头部区域 */
.register-header {
  background: linear-gradient(135deg, #52c41a 0%, #389e0d 100%);
  padding: 40px 30px 30px;
  text-align: center;
  position: relative;
  overflow: hidden;
}

.header-decoration {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
}

.circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
}

.circle-1 {
  width: 60px;
  height: 60px;
  top: -10px;
  right: -10px;
}

.register-title {
  color: white;
  font-size: 28px;
  font-weight: 700;
  margin: 0 0 8px 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  text-align: center;
}

.register-subtitle {
  color: rgba(255, 255, 255, 0.9);
  font-size: 14px;
  margin: 0;
  font-weight: 400;
  text-align: center;
}

/* 白色表单区域 */
.register-form {
  padding: 40px 30px;
  text-align: center;
}

:deep(.el-form) {
  text-align: center;
}

:deep(.el-form-item) {
  text-align: center;
  margin-bottom: 20px;
}

:deep(.el-form-item__label) {
  font-weight: 500;
  color: #606266;
  margin-bottom: 8px;
  text-align: center;
  display: block;
}

:deep(.el-form-item__content) {
  text-align: center;
  display: flex;
  justify-content: center;
}

:deep(.el-input) {
  width: 100%;
  max-width: 400px;
}

:deep(.el-input__wrapper) {
  border-radius: 8px;
  transition: all 0.3s ease;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 0 0 1px #52c41a;
}

:deep(.el-button) {
  border-radius: 8px;
  font-weight: 500;
  height: 48px;
  transition: all 0.3s ease;
  width: 100%;
  max-width: 400px;
}

:deep(.el-button--success) {
  background: linear-gradient(135deg, #52c41a 0%, #389e0d 100%);
  border: none;
}

:deep(.el-button--success:hover) {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(82, 196, 26, 0.3);
}

:deep(.el-button--success:disabled) {
  background: #c0c4cc;
  transform: none;
  box-shadow: none;
}

:deep(.el-checkbox) {
  text-align: center;
  justify-content: center;
}

/* 登录链接 */
.login-link {
  text-align: center;
  margin: 24px 0;
}

.login-text {
  font-size: 14px;
  text-decoration: none;
  text-align: center;
}

/* 新手指南 */
.newbie-guide {
  background: #f8f9fa;
  border-radius: 8px;
  padding: 16px;
  border-left: 4px solid #52c41a;
  text-align: center;
  max-width: 400px;
  margin: 0 auto;
}

.guide-header {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-bottom: 8px;
}

.guide-icon {
  color: #52c41a;
  font-size: 16px;
}

.guide-title {
  color: #52c41a;
  font-size: 14px;
  font-weight: 500;
  text-align: center;
}

.guide-content {
  color: #606266;
  font-size: 13px;
  line-height: 1.5;
  margin: 0;
  text-align: center;
}

@media (max-width: 768px) {
  .register-view {
    padding: 10px;
  }
  
  .register-container {
    margin: 10px auto;
  }
  
  .register-header {
    padding: 30px 20px 20px;
  }
  
  .register-form {
    padding: 30px 20px;
  }
  
  .register-title {
    font-size: 24px;
  }
  
  .register-subtitle {
    font-size: 12px;
  }
}

@media (max-width: 480px) {
  .register-view {
    padding: 10px;
  }
  
  .register-header {
    padding: 25px 15px 15px;
  }
  
  .register-form {
    padding: 25px 15px;
  }
  
  .register-title {
    font-size: 20px;
  }
  
  .newbie-guide {
    max-width: 100%;
  }
}
</style>
